<?php
/**
 * スケジュールデータマッパークラスモジュール
 */
require_once 'Request/DateTime.php';
require_once 'DateMath.php';
require_once APPLICATION_PATH . '/views/Mappers/ScheduleMapper.php';
require_once APPLICATION_PATH . '/views/Mappers/PlaceMapper.php';
require_once APPLICATION_PATH . '/models/Schedule/Controller.php';
require_once APPLICATION_PATH . '/models/Place/Controller.php';

/**
 * スケジュールデータマッパークラス
 * Enter description here ...
 * @author admin
 *
 */
class Components_Calendar_Mapper
{
	/** ハッシュキー：表示曜日 */
	const KEY_DISP_YOBI = 'DIP_YOBI';

	/** スケジュールデータセット */
	protected $_objDatasetSchedule;
	/** 集合場所データセット */
	protected $_objDatasetPlace;

	/**
	 * コンストラクタ
	 * Enter description here ...
	 * @param unknown_type $YYYYMM 年月
	 * 未設定の場合、ＲＥＱＵＥＳＴパラメータから取得
	 */
	public function __construct($YYYYMM='')
	{
		/*** 地図データオブジェクト初期化 */
		$this->_objDatasetPlace = new Mapper_Place();
		/*** 予定データオブジェクト初期化 */
		$this->_objDatasetSchedule = new Mapper_Schedule();
	}

	public function get($Age='')
	{
		$aryPlace = $this->_createPlaceDictionary();
		$aryDatasetSchedule = $this->_objDatasetSchedule->getArrayData($Age);
		if ( !is_array($aryDatasetSchedule) ) {
			$aryResult['LIST'] = array();
			return $aryResult;
		}

		$iListCount = count($aryDatasetSchedule['LIST']);
		$i = 0;
		while ( $i < $iListCount ) {
			/** 集合場所情報連結 */
			$PlaceId = $aryDatasetSchedule['LIST'][$i]['PLACE'];
			if ( !isset($aryPlace[$PlaceId]) ){
				$i ++;
				continue;
			}
			$aryDatasetSchedule['LIST'][$i]['PLACE_INFO'] = $aryPlace[$PlaceId];
			/** 学年情報編集 */
			$aryDatasetSchedule['LIST'][$i]['AGE_VIEW']
			= $this->_createAgeView($aryDatasetSchedule['LIST'][$i]['AGE']);
			/** 集合時刻変種湯 */
			$aryDatasetSchedule['LIST'][$i]['TIME_VIEW']
			= sprintf("%d", $aryDatasetSchedule['LIST'][$i]['HOUR'])
			.  ':'
			.  sprintf("%02d", $aryDatasetSchedule['LIST'][$i]['MINUTE']);
			/** 曜日追加 */
			$YYYYMMDDHHMM
			= $aryDatasetSchedule['LIST'][$i]['YEAR']
			.  sprintf("%02d", $aryDatasetSchedule['LIST'][$i]['MONTH'])
			.  sprintf("%02d", $aryDatasetSchedule['LIST'][$i]['DAY'])
			.  sprintf("%02d", $aryDatasetSchedule['LIST'][$i]['HOUR'])
			.  sprintf("%02d", $aryDatasetSchedule['LIST'][$i]['MINUTE']);
			$objDateMath = new DateMath(time(), TRUE);
			$iDayOfWeek
			= $objDateMath->getDayOfWeek($YYYYMMDDHHMM);
			$sDayOfWeek = $objDateMath->aryDayOfWeekJp[$iDayOfWeek];
			$aryDatasetSchedule['LIST'][$i]['DAY_OF_WEEK'] = $sDayOfWeek;
			/*** おしらせ */
			$aryDatasetSchedule['LIST'][$i]['NOTICE']
			= $this->_joinNotice($aryDatasetSchedule['LIST'][$i]['ID']);
			$aryDatasetSchedule['LIST'][$i]['NOTICE'] = '－';
			if ( isset($aryDatasetSchedule['LIST'][$i][Models_Schedule_Xml_Controller::KEY_INFO])) {
				if ( trim($aryDatasetSchedule['LIST'][$i][Models_Schedule_Xml_Controller::KEY_INFO]) != '') {
					$aryDatasetSchedule['LIST'][$i]['NOTICE'] = '有り';
				}
			}
			$i ++;
		}
		return $this->_sort($aryDatasetSchedule);
	}

	/**
	 * 連絡事項接続
	 * Enter description here ...
	 * @param unknown_type $Id
	 * @return string
	 */
	protected function _joinNotice($Id)
	{
		$sPathNotice
		= $this->_objDatasetSchedule->getDefaultSavePath() . '/notice/'.$Id.'.txt';
		if ( !file_exists($sPathNotice)) {
			return '－';
		}
		return '有り';
	}

	/**
	 * 集合日時でソート
	 * Enter description here ...
	 * @param unknown_type $aryDataset
	 * @return multitype:
	 */
	protected function _sort($aryDataset)
	{
		$aryPre = array();
		foreach ($aryDataset['LIST'] as $Key=>$Value) {
			$SortKey
			= $Value['YEAR']
			.  sprintf('%02d', $Value['MONTH'])
			.  sprintf('%02d', $Value['DAY'])
			.  sprintf('%02d', $Value['HOUR'])
			.  sprintf('%02d', $Value['MINUTE']);
			$aryPre[$Key] = $SortKey;
		}
		asort($aryPre);
		$aryResult['LIST'] = array();
		foreach ($aryPre as $Key=>$Value) {
			$aryResult['LIST'][] = $aryDataset['LIST'][$Key];
		}

		return $aryResult;
	}

	protected function _createAgeView($AGE)
	{
		$AgeViewDictinary
		= array(
			'U7'=>'１年生',
			'U8'=>'２年生',
			'U9'=>'３年生',
			'U10'=>'４年生',
			'U11'=>'５年生',
			'U12'=>'６年生',
			'U13'=>'１年生',
			'U14'=>'２年生',
			'U15'=>'３年生',
			'U16'=>'１年生',
			'U17'=>'２年生',
			'U18'=>'３年生',
		);

		$aryAge = explode(',', $AGE);
		$aryAgeView = array();
		foreach( $aryAge as $sAge ) {
			if ( !isset($AgeViewDictinary[$sAge])) {
				continue;
			}
			$aryAgeView[]=$AgeViewDictinary[$sAge];
		}

		$sResult = implode('<br />', $aryAgeView);
		return $sResult;
	}

	protected function _createPlaceDictionary()
	{
		$aryResult = array();
		$aryDatasetPlace = $this->_objDatasetPlace->getArrayData();
		foreach ( $aryDatasetPlace['LIST'] as $PlaceInfo ) {
			$aryResult[$PlaceInfo['ID']] = $PlaceInfo;
		}
		return $aryResult;
	}
}